package com.luo.gmall.cart.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.luo.gmall.cart.entity.Cart;
import com.luo.gmall.cart.mapper.CartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Zoro
 * @date 2021年11月23
 */
@Service
public class CartAsyncService {

    @Autowired
    private CartMapper cartMapper;


    /**
     * 更改cart
     *
     * @param cart
     * @param userId
     */
    @Async
    public void updateCart(String userId, Cart cart) {
        cartMapper.update(cart, new UpdateWrapper<Cart>().eq("user_id", userId).eq("sku_id", cart.getSkuId()));
    }


    @Async
    public void insertCart(String userId, Cart cart) {
//        int i = 1 / 0;
        cartMapper.insert(cart);
    }


    @Async
    public void deleteCarts(String userId) {
        cartMapper.delete(new QueryWrapper<Cart>().eq("user_id", userId));
    }

    @Async
    public void deleteCart(String userId, Long skuId) {
        cartMapper.delete(new QueryWrapper<Cart>().eq("user_id", userId).eq("sku_id", skuId));
    }

}
